package com.example.administrator.radarplusfriendsdemo.custom;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.radarplusfriendsdemo.R;
import com.example.administrator.radarplusfriendsdemo.been.Info;
import com.example.administrator.radarplusfriendsdemo.utils.DisplayUtils;

import de.hdodenhof.circleimageview.CircleImageView;

public class CircleView extends RelativeLayout {

    private Info info;//每个头像对应的信息
    private AutoScaleTextView tvTitle;//对应的标题
    private CircleImageView civHead;//对应的头像

    public CircleView(Context context) {
        this(context,null);
    }

    public CircleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        //1.加载页面布局，同时将页面布局加入到容器中
        View view = View.inflate(getContext(),R.layout.circleview_item_layout,this);
        //2.找到控件
        tvTitle = view.findViewById(R.id.astv_title);//标题
        civHead = view.findViewById(R.id.civ_head); //头像
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measureSize(widthMeasureSpec), measureSize(heightMeasureSpec));
    }

    private int measureSize(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = DisplayUtils.dp2px(getContext(),18);
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

    public void setInfo(Info info) {
        this.info = info;

        if (info != null && tvTitle != null && civHead != null){
            if (info.getType() == Info.TYPE_NONE){//什么标识都没有
                tvTitle.setVisibility(INVISIBLE);
            }else if (info.getType() == Info.TYPE_WO_QING){//我请
                tvTitle.setVisibility(VISIBLE);
                tvTitle.setBackgroundResource(R.drawable.msg_blue);
                tvTitle.setText(R.string.wo_qing);
            }else if(info.getType() == Info.TYPE_QING_WO){//请我
                tvTitle.setVisibility(VISIBLE);
                tvTitle.setText(R.string.qing_wo);
                tvTitle.setBackgroundResource(R.drawable.msg_red);
            }else{
                tvTitle.setVisibility(INVISIBLE);
            }
            civHead.setImageResource(info.getPortraitId());
        }
    }

    public Info getInfo() {
        return info;
    }
}
